/**
 * 数据集控制器
 */
cBoard.controller('datasetCtrl', function ($scope, $http, dataService, $uibModal, ModalUtils, $filter, chartService, $timeout, uuid4) {

    var translate = $filter('translate');
    $scope.optFlag = 'none';
    $scope.curDataset = {data: {expressions: [], filters: [], schema: {dimension: [], measure: []}}};
    $scope.curWidget = {};
    $scope.alerts = [];
    $scope.verify = {dsName: true};
    $scope.loadFromCache = true;
    $scope.queryAceOpt = cbAcebaseOption;
    $scope.hierarchy = translate("CONFIG.DATASET.HIERARCHY");
    $scope.uuid4 = uuid4;

    var treeID = 'dataSetTreeID'; // Set to a same value with treeDom
    var originalData = [];
    var updateUrl = "dashboard/updateDataset.do";

    var trash = {};
/**
 * 删除数据集
 * */
    $scope.toTrash = function (array, index) {
        var o = array[index];
        if (o.type == 'column') {
            if (!trash[o.column]) {
                trash[o.column] = [];
            }
            trash[o.column].push(o);
        }
        array.splice(index, 1);
    };
    /**
     * 设置数据集取消功能
     * */
    $scope.cancel=function(){
        $scope.optFlag = 'none';
    }
    $scope.dndTransfer = {
        dimension: function (list, index, item, type) {
            if (type == 'column') {
                list[index] = {type: 'column', column: item};
            }
        },
        measure: function (list, index, item, type) {
            if (type == 'column') {
                list[index] = {type: 'column', column: item};
            }
        }
    };
    /**
     * 获取数据源
     * */
    $http.get("dashboard/getDatasourceList.do").success(function (response) {
        $scope.datasourceList = response;
    });
    /**
     * 获取数据集列表
     * */
    var getDatasetList = function () {
        $http.get("dashboard/getDatasetList.do").success(function (response) {
               $scope.datasetList = response;

            $scope.searchNode();
        });

    };
    /**
     * 获取目录信息
     * */
    var getCategoryList = function () {
        $http.get("dashboard/getDatasetCategoryList.do").success(function (response) {
            $scope.categoryList = response;
            $("#DatasetName").autocomplete({
                source: $scope.categoryList
            });
        });
    };

    getCategoryList();
    getDatasetList();
    /**
     * 新建数据源
     * */
    $scope.newDs = function () {
        $scope.optFlag = 'new';
        $scope.curDataset = {data: {expressions: [], filters: [], schema: {dimension: [], measure: []}}};
        $scope.curWidget = {};
        $scope.datasource="";
        $scope.curDataset.name="";
        $scope.selects=false;
        $scope.paramList=[];
        cleanPreview();
    };
    /**
     * 修改数据源
     * */
    $scope.editDs = function (ds) {
        $http.post("dashboard/checkDatasource.do", {id: ds.data.datasource}).success(function (response) {
            if (response.status == '1') {
                doEditDs(ds);
            } else {
                ModalUtils.alert(translate("ADMIN.CONTACT_ADMIN") + "：Datasource/" + response.msg, "modal-danger", "lg");
            }
        });
    };

    var doEditDs = function (ds) {
        $scope.optFlag = 'edit';
        $scope.curDataset = angular.copy(ds);
        $scope.paramList =$scope.curDataset.data.query.parameters;
       // $scope.paramList = $scope.curWidget.query.parameters;
        $scope.curDataset.name = $scope.curDataset.categoryName + '/' + $scope.curDataset.name;
        if (!$scope.curDataset.data.expressions) {
            $scope.curDataset.data.expressions = [];
        }
        if (!$scope.curDataset.data.filters) {
            $scope.curDataset.data.filters = [];
        }
        if (!$scope.curDataset.data.schema) {
            $scope.curDataset.data.schema = {dimension: [], measure: []};
        }
        $scope.datasource = _.find($scope.datasourceList, function (ds) {
            return ds.id == $scope.curDataset.data.datasource;
        });
        $scope.curWidget.query = $scope.curDataset.data.query;
        $scope.loadData();
    };

    $scope.checkExist = function (column) {
        var find = _.find($scope.curDataset.data.schema.measure, function (e) {
            return e.column == column;
        });
        if (!_.isUndefined(find)) {
            return true;
        }
        find = _.find($scope.curDataset.data.schema.dimension, function (e) {
            if (e.type == 'level') {
                var _find = _.find(e.columns, function (_e) {
                    return _e.column == column;
                });
                return !_.isUndefined(_find);
            } else {
                return e.column == column;
            }
        });
        return !_.isUndefined(find);
    };

    $scope.deleteDs = function (ds) {
        $http.get("dashboard/getAllWidgetList.do").then(function (response) {
            if (!response) {
                return false;
            }
            var resDs = [];

            for (var i = 0; i < response.data.length; i++) {
                if (response.data[i].data.datasetId == ds.id) {
                    resDs.push(response.data[i].name);
                }
            }

            if (resDs.length > 0) {
                var warnStr = translate("CONFIG.WIDGET.WIDGET") + ":[" + resDs.toString() + "]";
                ModalUtils.alert(translate("COMMON.NOT_ALLOWED_TO_DELETE_BECAUSE_BE_DEPENDENT") + warnStr, "modal-warning", "");
                return false;
            }
            ModalUtils.confirm(translate("COMMON.CONFIRM_DELETE"), "modal-warning", "", function () {
                $http.post("dashboard/deleteDataset.do", {id: ds.id}).success(function (serviceStatus) {
                    if (serviceStatus.status == '1') {
                        getDatasetList();
                    } else {
                        ModalUtils.alert(serviceStatus.msg, "modal-warning", "lg");
                    }
                    $scope.optFlag = 'none';
                });
            });
        });
    };

    $scope.copyDs = function (ds) {
        var data = angular.copy(ds);
        data.name = data.name + "_copy";
        $http.post("dashboard/saveNewDataset.do", {json: angular.toJson(data)}).success(function (serviceStatus) {
            if (serviceStatus.status == '1') {
                $scope.optFlag = 'none';
                getDatasetList();
                ModalUtils.alert(translate("COMMON.SUCCESS"), "modal-success", "sm");
            } else {
                ModalUtils.alert(serviceStatus.msg, "modal-warning", "lg");
            }
        });
    };

    var validate = function () {
        $scope.alerts = [];
        if (!$scope.curDataset.name) {
            $scope.alerts = [{msg: translate('CONFIG.DATASET.NAME') + translate('COMMON.NOT_EMPTY'), type: 'danger'}];
            $scope.verify = {dsName: false};
            $("#DatasetName").focus();
            return false;
        }
        if (!$scope.datasource) {
            $scope.alerts = [{msg: translate('CONFIG.DATASET.DATASOURCE') + translate('COMMON.NOT_EMPTY'), type: 'danger'}];
            //$scope.verify = {datasource: false};
            //$("#DatasetName").focus();
            return false;
        }
        if(!$scope.curWidget.query.sql){
            $scope.alerts = [{msg: "请输入sql查询语句", type: 'danger'}];
            //$scope.verify = {datasource: false};
            //$("#DatasetName").focus();
            return false;
        }
       // var param = $scope.curWidget.query.parameters;
        var sql = $scope.curWidget.query.sql;
        var reg = /(\${[^{]*\})/g;
        var strArr = sql.match(reg);
        var size =  _.size(strArr);
        if(size==0){
            $scope.paramList =[];
        }else{
           if(!$scope.paramList || $scope.paramList.length==0){
               $scope.alerts = [{msg: '请点击获取动态参数按钮', type: 'danger'}];
               return false;
           }
            var flag = true;
            if($scope.paramList){
                var len = $scope.paramList.length;
                for(var i =0;i < len;i++){
                    if($scope.paramList[i].text==""){
                        $scope.alerts = [{msg: '请设置动态参数标题', type: 'danger'}];
                        flag = false;
                        break;
                    }
                    if($scope.paramList[i].formElement==""){
                        $scope.alerts = [{msg: '请设置动态参数表单类型', type: 'danger'}];
                        flag = false;
                        break;
                    }
                    if($scope.paramList[i].formElement =="text" || $scope.paramList[i].formElement =="date"){
                        if($scope.paramList[i].dataType==""){
                            $scope.alerts = [{msg: '请设置动态参数表单数据类型', type: 'danger'}];
                            flag = false;
                            break;
                        }
                        if($scope.paramList[i].defaultValue==""){
                            $scope.alerts = [{msg: '请设置动态参数表单默认值', type: 'danger'}];
                            flag = false;
                            break;
                        }
                    }else if($scope.paramList[i].formElement =="select"){
                        if($scope.paramList[i].dataType==""){
                            $scope.alerts = [{msg: '请设置动态参数表单数据类型', type: 'danger'}];
                            flag = false;
                            break;
                        }
                        if($scope.paramList[i].dataSource==""){
                            $scope.alerts = [{msg: '请设置动态参数表单数据来源', type: 'danger'}];
                            flag = false;
                            break;
                        }
                        if($scope.paramList[i].content==""){
                            $scope.alerts = [{msg: '请设置动态参数内容', type: 'danger'}];
                            flag = false;
                            break;
                        }
                        if($scope.paramList[i].defaultValue==""){
                            $scope.alerts = [{msg: '请设置动态参数表单默认值', type: 'danger'}];
                            flag = false;
                            break;
                        }
                    }else if($scope.paramList[i].formElement =="tree"){
                        if($scope.paramList[i].content==""){
                            $scope.alerts = [{msg: '请输入获取树节点的sql', type: 'danger'}];
                            flag = false;
                            break;
                        }
                        if($scope.paramList[i].defaultValue==""){
                            $scope.alerts = [{msg: '请设置动态参数表单默认值', type: 'danger'}];
                            flag = false;
                            break;
                        }
                    }
                }
                return flag;
            }
        }
        return true;
    };

    $scope.save = function () {
        if (!validate()) {
            return;
        }
        $scope.datasource ? $scope.curDataset.data.datasource = $scope.datasource.id : null;
        $scope.curDataset.data.query = $scope.curWidget.query;
        $scope.curDataset.data.query.parameters =$scope.paramList;

        var ds = angular.copy($scope.curDataset);
        var index = ds.name.lastIndexOf('/');
        ds.categoryName = $scope.curDataset.name.substring(0, index).trim();
        ds.name = $scope.curDataset.name.slice(index + 1).trim();
        if (ds.categoryName == '') {
            ds.categoryName = translate("COMMON.DEFAULT_CATEGORY");
        }

        if ($scope.optFlag == 'new') {
            $http.post("dashboard/saveNewDataset.do", {json: angular.toJson(ds)}).success(function (serviceStatus) {
                if (serviceStatus.status == '1') {
                    getCategoryList();
                    getDatasetList();
                    $scope.verify = {dsName: true};
                    ModalUtils.alert(translate("COMMON.SUCCESS"), "modal-success", "sm");
                } else {
                    $scope.alerts = [{msg: serviceStatus.msg, type: 'danger'}];
                }
            });
        } else {
            $http.post(updateUrl, {json: angular.toJson(ds)}).success(function (serviceStatus) {
                if (serviceStatus.status == '1') {
                    $scope.optFlag = 'edit';
                    getCategoryList();
                    getDatasetList();
                    $scope.verify = {dsName: true};
                    ModalUtils.alert(translate("COMMON.SUCCESS"), "modal-success", "sm");
                } else {
                    $scope.alerts = [{msg: serviceStatus.msg, type: 'danger'}];
                }
            });
        }

    };

    $scope.editFilterGroup = function (col) {
        var selects = schemaToSelect($scope.curDataset.data.schema);
        $uibModal.open({
            templateUrl: 'org/cboard/view/config/modal/filterGroup.html',
            windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
            backdrop: false,
            scope: $scope,
            controller: function ($scope, $uibModalInstance) {
                if (col) {
                    $scope.data = angular.copy(col);
                } else {
                    $scope.data = {group: '', filters: [], id: uuid4.generate()};
                }
                $scope.selects = selects;
                $scope.close = function () {
                    $uibModalInstance.close();
                };
                $scope.addColumn = function (str) {
                    $scope.data.filters.push({col: str, type: '=', values: []})
                };
                $scope.ok = function () {
                    if (col) {
                        col.group = $scope.data.group;
                        col.filters = $scope.data.filters;
                    } else {
                        if ($scope.$parent.curDataset.data.filters == null) {
                            $scope.$parent.curDataset.data.filters = [];
                        }
                        $scope.$parent.curDataset.data.filters.push($scope.data);
                    }
                    $uibModalInstance.close();
                };
                $scope.editFilter = function (filter) {
                    $uibModal.open({
                        templateUrl: 'org/cboard/view/dashboard/modal/param.html',
                        windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
                        backdrop: false,
                        size: 'lg',
                        resolve: {
                            param: function () {
                                return angular.copy(filter);
                            },
                            filter: function () {
                                return false;
                            },
                            getSelects: function () {
                                return function (byFilter, column, callback) {
                                    dataService.getDimensionValues($scope.datasource.id, $scope.curWidget.query, undefined, column, undefined, function (filtered) {
                                        callback(filtered);
                                    });
                                };
                            },
                            ok: function () {
                                return function (param) {
                                    filter.type = param.type;
                                    filter.values = param.values;
                                }
                            }
                        },
                        controller: 'paramSelector'
                    });
                };
            }
        });
    };

    $scope.deleteFilterGroup = function (index) {
        ModalUtils.confirm(translate("COMMON.FILTER_GROUP") + ": [" + $scope.curDataset.data.filters[index].group + "], " +
            translate("COMMON.CONFIRM_DELETE"), "modal-warning", "",
            function () {
                $scope.curDataset.data.filters.splice(index, 1)
            }
        );
    };

    var schemaToSelect = function (schema) {
        if (schema.selects) {
            return angular.copy(schema.selects);
        } else {
            var selects = [];
            selects = selects.concat(schema.measure);
            _.each(schema.dimension, function (e) {
                if (e.type == 'level') {
                    _.each(e.columns, function (c) {
                        selects.push(c);
                    });
                } else {
                    selects.push(e);
                }
            });
            return angular.copy(selects);
        }
    };

    $scope.editExp = function (col) {
        var selects = schemaToSelect($scope.curDataset.data.schema);
        var aggregate = [
            {name: 'sum', value: 'sum'},
            {name: 'count', value: 'count'},
            {name: 'avg', value: 'avg'},
            {name: 'max', value: 'max'},
            {name: 'min', value: 'min'}
        ];
        var ok;
        var data = {expression: ''};
        if (!col) {
            ok = function (exp, alias) {
                $scope.curDataset.data.expressions.push({
                    type: 'exp',
                    exp: data.expression,
                    alias: data.alias,
                    id: uuid4.generate()
                });
            }
        } else {
            data.expression = col.exp;
            data.alias = col.alias;
            ok = function (data) {
                col.exp = data.expression;
                col.alias = data.alias;
            }
        }

        $uibModal.open({
            templateUrl: 'org/cboard/view/config/modal/exp.html',
            windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
            backdrop: false,
            size: 'lg',
            controller: function ($scope, $uibModalInstance) {
                $scope.data = data;
                $scope.selects = selects;
                $scope.aggregate = aggregate;
                $scope.alerts = [];
                $scope.expAceOpt = expEditorOptions(selects, aggregate);

                $scope.close = function () {
                    $uibModalInstance.close();
                };
                $scope.addToken = function (str, agg) {
                    var tc = document.getElementById("expression_area");
                    var tclen = $scope.data.expression.length;
                    tc.focus();
                    var selectionIdx = 0;
                    if (typeof document.selection != "undefined") {
                        document.selection.createRange().text = str;
                        selectionIdx = str.length - 1;
                    }
                    else {
                        var a = $scope.data.expression.substr(0, tc.selectionStart);
                        var b = $scope.data.expression.substring(tc.selectionStart, tclen);
                        $scope.data.expression = a + str;
                        selectionIdx = $scope.data.expression.length - 1;
                        $scope.data.expression += b;
                    }
                    if (!agg) {
                        selectionIdx++;
                    }
                    tc.selectionStart = selectionIdx;
                    tc.selectionEnd = selectionIdx;
                };
                $scope.verify = function () {
                    $scope.alerts = [];
                    var v = verifyAggExpRegx($scope.data.expression);
                    $scope.alerts = [{
                        msg: v.isValid ? translate("COMMON.SUCCESS") : v.msg,
                        type: v.isValid ? 'success' : 'danger'
                    }];
                };
                $scope.ok = function () {
                    if (!$scope.data.alias) {
                        ModalUtils.alert(translate('CONFIG.WIDGET.ALIAS') + translate('COMMON.NOT_EMPTY'), "modal-warning", "lg");
                        return;
                    }
                    ok($scope.data);
                    $uibModalInstance.close();
                };
            }
        });
    };

    $scope.deleteExp = function (index) {
        ModalUtils.confirm(translate("CONFIG.COMMON.CUSTOM_EXPRESSION") + ": [" + $scope.curDataset.data.expressions[index].alias + "], " +
            translate("COMMON.CONFIRM_DELETE"), "modal-warning", "",
            function () {
                $scope.curDataset.data.expressions.splice(index, 1)
            }
        );
    };

    $scope.createNode = function (item) {
        if (trash[item.column]) {
            var _i = trash[item.column].pop();
            if (_i) {
                return _i;
            }
        }
        item.id = uuid4.generate();
        return item;
    };

    $scope.measureToDimension = function (index, o) {
        $scope.curDataset.data.schema.measure.splice(index, 1);
        $scope.curDataset.data.schema.dimension.push(o);
    };

    $scope.toDimension = function (o) {
        $scope.curDataset.data.schema.dimension.push($scope.createNode(o));
    };

    $scope.custom = function (o) {
        var selects = $scope.selects;
        var datasource = $scope.datasource;
        $uibModal.open({
            templateUrl: 'org/cboard/view/config/modal/custom.html',
            windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
            backdrop: false,
            size: 'lg',
            controller: function ($scope, $uibModalInstance) {
                $scope.c = o;
                $scope.ok = function () {
                    $uibModalInstance.close();
                };
                $scope.customAceOpt = schemaCustomOpt(selects, datasource.type);
            }
        });
    };

    $scope.loadData = function () {
        cleanPreview();
        $scope.loading = true;
        if(!$scope.datasource){
            $scope.loading = false;
            $scope.alerts = [{msg: "请选择数据源", type: 'danger'}];
            return false;
        }

        dataService.getColumns({
            datasource: $scope.datasource.id,
            query: $scope.curWidget.query,
            datasetId: null,
            reload: !$scope.loadFromCache,
            callback: function (dps) {
                $scope.alerts=[];
                $scope.loading = false;
                $scope.toChartDisabled = false;
                if (dps.msg == "1") {
                    $scope.alerts = [];

                    $scope.selects = dps.columns;
                } else {
                    if(dps.msg){
                        $scope.alerts = [{msg: dps.msg, type: 'danger'}];
                    }

                }

                var widget = {
                    chart_type: "table",
                    filters: [],
                    groups: [],
                    keys: [],
                    selects: [],
                    values: [{
                        cols: []
                    }
                    ]
                };
                _.each($scope.selects, function (c) {
                    widget.keys.push({
                        col: c,
                        type: "eq",
                        values: []
                    });
                });
            }
        });
    };

    var cleanPreview = function () {
        $('#dataset_preview').html("");
    };

    /**  js tree related start **/
    $scope.treeConfig = jsTreeConfig1;

    $("#" + treeID).keyup(function (e) {
        if (e.keyCode == 46) {
            $scope.deleteNode();
        }
    });

    var getSelectedDataSet = function () {
        var selectedNode = jstree_GetSelectedNodes(treeID)[0];
        return _.find($scope.datasetList, function (ds) {
            return ds.id == selectedNode.id;
        });
    };

    var checkTreeNode = function (actionType) {
        return jstree_CheckTreeNode(actionType, treeID, ModalUtils.alert);
    };

    var switchNode = function (id) {
        $scope.ignoreChanges = false;
        var dataSetTree = jstree_GetWholeTree(treeID);
        dataSetTree.deselect_all();
        dataSetTree.select_node(id);
    };

    $scope.applyModelChanges = function () {
        return !$scope.ignoreChanges;
    };

    $scope.copyNode = function () {
        if (!checkTreeNode("copy")) return;
        $scope.copyDs(getSelectedDataSet());
    };

    $scope.editNode = function () {
        if (!checkTreeNode("edit")) return;
        $scope.editDs(getSelectedDataSet());
    };

    $scope.deleteNode = function () {
        if (!checkTreeNode("delete")) return;
        $scope.deleteDs(getSelectedDataSet());
    };
    $scope.searchNode = function () {
        var para = {dsName: '', dsrName: ''};
        //map datasetList to list (add datasourceName)
        var list = $scope.datasetList.map(function (ds) {
            var dsr = _.find($scope.datasourceList, function (obj) {
                return obj.id == ds.data.datasource
            });
            return {
                "id": ds.id,
                "name": ds.name,
                "categoryName": ds.categoryName,
                "datasourceName": dsr ? dsr.name : ''
            };
        });
        //split search keywords
        if ($scope.keywords) {
            if ($scope.keywords.indexOf(' ') == -1 && $scope.keywords.indexOf(':') == -1) {
                para.dsName = $scope.keywords;
            } else {
                var keys = $scope.keywords.split(' ');
                for (var i = 0; i < keys.length; i++) {
                    var w = keys[i].trim();
                    if (w.split(':')[0] == 'ds') {
                        para["dsName"] = w.split(':')[1];
                    }
                    if (w.split(':')[0] == 'dsr') {
                        para["dsrName"] = w.split(':')[1];
                    }
                }
            }
        }
        //filter data by keywords
        originalData = jstree_CvtVPath2TreeData(
            $filter('filter')(list, {name: para.dsName, datasourceName: para.dsrName})
        );

        jstree_ReloadTree(treeID, originalData);
    };

    $scope.treeEventsObj = function () {
        var baseEventObj = jstree_baseTreeEventsObj({
            ngScope: $scope, ngHttp: $http, ngTimeout: $timeout,
            treeID: treeID, listName: "datasetList", updateUrl: updateUrl
        });
        return baseEventObj;
    }();

    /**  js tree related end **/


    /** Ace Editor Starer... **/
    $scope.queryAceOpt = datasetEditorOptions();
    var getParamByName = function(name){
        var obj =null;
        _.each($scope.curWidget.query.parameters,function(e){
            if(e.name == name){
                obj = e;
                return false;
            }
        });
        return obj;
    }

    $scope.getParam=function(){
        if(!$scope.datasource){
            $scope.loading = false;
            $scope.toChartDisabled = false;
            $scope.alerts = [{msg: "请选择数据源", type: 'danger'}];
            return false;
        }
        //  var sql = $scope.curDataset.data.query.sql;
        var param = $scope.curWidget.query.parameters;
        var sql = $scope.curWidget.query.sql;

        if(!sql ||sql == ""){
            $scope.alerts = [{msg: translate('CONFIG.DATASET.SQL_TEXT_EMPTY'), type: 'danger'}];
            $scope.loading = false;
            $scope.toChartDisabled = false;
            return;
        }
        var reg = /(\${[^{]*\})/g;
        var strArr = sql.match(reg);
        var size =  _.size(strArr);
        if(size!=0){
            strArr=_.uniq(strArr);
            $scope.paramList=[];
            $scope.paramTable=true;
            _.each(strArr,function(e){
                var name = e.replace("${","").replace("}","");
                var paramJson = getParamByName(name);
                if(paramJson){
                    $scope.paramList.push(paramJson);
                }else{
                    var json = {"name":name,"text":"","formElement":"",
                        "dataType":"","dataSource":"","content":"","defaultValue":""};
                    $scope.paramList.push(json);
                }


            });
        }else{
            $scope.paramList=[];
            $scope.paramTable=false;
        }

    }

    $scope.editParam=function(param){
        $scope.optParamFlag ="edit";
       // console.log(param);
        $uibModal.open({
            templateUrl: 'org/cboard/view/config/modal/dyparam.html',
            windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
            backdrop: false,
            controller: function ($scope, $uibModalInstance) {
                $scope.parameter = param;
                if(param.formElement=="select"){
                    $scope.showContent=true;
                    $scope.treeFlag = false;
                }else{
                    $scope.treeFlag = false;
                    $scope.showContent=false;
                }
                if(param.formElement=="tree"){
                    $scope.treeFlag = true;
                    $scope.showContent=true;
                }
                var type = $scope.parameter.dataSource;
                if(type=="text"){
                    $scope.sourceTip='text格式为[{"key":1,"value":"河北建投"}],默认值为{"key":1,"value":"河北建投"}';
                }if(type=="sql"){
                    $scope.sourceTip='sql格式：select col1 as name,col2 as text from table,默认值为{"key":1,"value":"河北建投"}';
                }else{
                    $scope.sourceTip="";
                }
                $scope.close = function () {
                    $uibModalInstance.close();
                };
                $scope.ok=function(){
                    $scope.alerts = [];
                    if (!$scope.parameter.name) {
                        $scope.alerts.push({msg:"查询参数名称" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                        return;
                    }
                    if (!$scope.parameter.text) {
                        $scope.alerts.push({msg:"标题" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                        return;
                    }
                    if (!$scope.parameter.formElement) {
                        $scope.alerts.push({msg:"表单类型" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                        return;
                    }
                    if(param.formElement!="tree"){

                        if (!$scope.parameter.dataType) {
                            $scope.alerts.push({msg:"数据类型" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                            return;
                        }
                        if(param.formElement=="select"){
                            if(!$scope.parameter.dataSource){
                                $scope.alerts.push({msg:"数据来源" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                                return;
                            }
                            if(!$scope.parameter.content){
                                $scope.alerts.push({msg:"内容" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                                return;
                            }

                            if(!stringToJson($scope.parameter.content) && $scope.parameter.dataSource == "text"){
                                $scope.alerts.push({msg:"内容格式不正确",type:"danger"});
                                return;
                            }

                        }
                    }else{
                        $scope.parameter.dataSource ="";
                        if(!$scope.parameter.content){
                            $scope.alerts.push({msg:"内容" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                            return;
                        }

                        if(!stringToJson($scope.parameter.content) && $scope.parameter.dataSource == "text"){
                            $scope.alerts.push({msg:"内容格式不正确",type:"danger"});
                            return;
                        }
                    }
                    var defautlt = "";
                    if($scope.parameter.defaultValue){
                        defautlt = $scope.parameter.defaultValue;
                    }else{
                        defautlt = $("#defaultValue").val();
                    }
                    if (!defautlt) {
                        $scope.alerts.push({msg:"默认值" + translate('COMMON.NOT_EMPTY'),type:"danger"});
                        return;
                    }
                    setParamList($scope.parameter);
                    $uibModalInstance.close();
                };
                $scope.changeType = function(){
                    var type = $scope.parameter.formElement;
                    $scope.treeFlag = false;
                    $scope.showContent=false;
                    if(type=="select"){
                        $scope.showContent=true;
                        $scope.treeFlag = false;
                        $scope.parameter.content="";

                    }else if(type=="tree"){
                        $scope.treeFlag = true;
                        $scope.parameter.content="";
                    }else{
                        $scope.treeFlag = false;
                        $scope.showContent=false;
                        $scope.parameter.content="";

                    }
                }
                $scope.changeSourceType = function(){
                    var type = $scope.parameter.dataSource;

                    $scope.sourceTip="";
                    if(type=="text"){
                        $scope.sourceTip='text格式为[{"key":1,"value":"河北建投"}],默认值为{"key":1,"value":"河北建投"}'
                    }else if(type=="sql"){
                        $scope.sourceTip='sql格式：select col1 as name,col2 as text from table,默认值为{"key":1,"value":"河北建投"}';
                    }else{
                        $scope.sourceTip="";
                    }
                }

            }
        });
        var stringToJson = function(value){
            var flag = true;
            try{
                // eval('('+value+')');
                angular.fromJson(value);
                return flag;
            }
            catch(er){
                flag = false;
                return flag;
            }
        }
        var setParamList = function(params){
            if(params.formElement=="date"){
                params.defaultValue =$("#defaultValue").val();
            }
            if(params.formElement=="tree"){
                params.dataSource="sql";
            }
            if(params.formElement == "text"){
                params.dataSource ="";
                params.content="";
            }
            var json = {"name":params.name,"text":params.text,"formElement":params.formElement,
                "dataType":params.dataType,"dataSource":params.dataSource,"content":params.content,"defaultValue":params.defaultValue}
            if($scope.optParamFlag=="add"){
                $scope.paramList.push(json);
            }else{
                _.each($scope.paramList,function(obj){
                    if(obj == json){
                        $scope.paramList.remove(obj);
                        $scope.paramList.add(json);
                    }
                })
            }
            $scope.paramTable=true;
        }

    }
$scope.delParam=function(p){
    // $scope.paramList.splice(in);
    var len = $scope.paramList.length;
    for(var i = 0;i < len ;i++){
        if($scope.paramList[i]==p){

            $scope.paramList.splice(i,1);
            //
            // break;
        }
    }
    _.size($scope.paramList)==0?$scope.paramTable=true:$scope.paramTable=false;

}
});